package com.macrounion.atv.config;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.web.servlet.LocaleResolver;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.macrounion.atv.cache.DeviceIdQueue;
import com.macrounion.atv.dto.req.SnmpGetRouterReqDto;
import com.macrounion.atv.listener.SnmpGetDeviceDataListener;
import com.macrounion.atv.listener.SnmpTrapDeviceDataListener;
import com.macrounion.device.snmp.Snmp1;
import com.macrounion.device.snmp.config.SnmpConfig;

@Configuration
public class Beans {
	
	@Value("${Snmp.ip:auto}")	
	private String snmpIp;
	
	@Autowired
	private SnmpGetDeviceDataListener snmpGetDeviceDataListener;
	@Autowired
	private SnmpTrapDeviceDataListener snmpTrapDeviceDataListener;
	
	@Bean
    public ResourceBundleMessageSource messageSource(){
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("i18n.messages");
        return messageSource;
    }
	
	@Bean
    public LocaleResolver localeResolver() {
        return new SwitchLocalResolver();
    }

	@Bean
	@ConfigurationProperties(prefix = "file")
	public FileConfig fileConfig(){
		return new FileConfig();
	}
	
	@Bean
	public Snmp1<SnmpGetRouterReqDto> Snmp() {
		SnmpConfig config = new SnmpConfig();
		if(!snmpIp.equals("auto"))
			config.setIp(snmpIp);
		Snmp1<SnmpGetRouterReqDto> snmp = new Snmp1<SnmpGetRouterReqDto>(snmpGetDeviceDataListener,config);
		snmp.setTrapListener(snmpTrapDeviceDataListener);
		snmp.start();
		return snmp;
	}

    /**
     * 初始注入scheduler
     * @return
     * @throws SchedulerException
     */
    @Bean
    public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean) throws SchedulerException{
         return schedulerFactoryBean.getScheduler();
    }
    
    @Bean
    public MyJobFactory jobFactory(){
         return new MyJobFactory();
    }
    
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(MyJobFactory jobFactory){
         SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
         schedulerFactoryBean.setJobFactory(jobFactory);
         return schedulerFactoryBean;
    }
    
    @Value("${timely.receive}")
    private int timeout;
    
	@Bean LoadingCache<String,Date> getTimelyServerTimeCache(){
		return CacheBuilder.newBuilder().maximumSize(3000).
				expireAfterWrite(timeout, TimeUnit.SECONDS)
				.build(
					new CacheLoader<String, Date>() {
						@Override
						public Date load(String key) throws Exception {
							return new Date();
						}
					}
				);
	}
	
	/**
	 * 存储正在处理
	 * 线程安全的
	 * @return
	 */
	@Bean DeviceIdQueue getDeviceThreadRunningIds(){
		return new DeviceIdQueue();
	}
}
